package com.example.qxfw.mongodb.hy.service.impl;

import com.example.qxfw.mongodb.hy.entity.HyDocFile;
import com.example.qxfw.mongodb.hy.service.DocFileService;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
public class DocFileServiceImpl implements DocFileService {

    @Qualifier("mongoSecondTemplate")
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public HyDocFile getById(String id) {
        return mongoTemplate.findOne(new Query(Criteria.where("id").is(id)),HyDocFile.class);
    }

    @Override
    public HyDocFile getByNameAndId(String tableName, String id) {
        return mongoTemplate.findOne(new Query(
                Criteria.where("id").is(id)
                        .and("table_name").is(tableName)),HyDocFile.class);
    }

    @Override
    public Long deleteById(String id) {
        DeleteResult deleteResult = mongoTemplate.remove(new Query(Criteria.where("_id").is(id)), HyDocFile.class);
        return deleteResult.getDeletedCount();
    }

    @Override
    public Long deleteByTableIdAndName(String tableId, String tableName) {
        DeleteResult deleteResult = mongoTemplate.remove(new Query(Criteria.where("id").is(tableId)
                .and("table_name").is(tableName)), HyDocFile.class);
        return deleteResult.getDeletedCount();
    }

    @Override
    public HyDocFile getByTableId(String id) {
        return mongoTemplate.findOne(new Query(Criteria.where("id").is(id)),HyDocFile.class);
    }

    @Override
    public HyDocFile save(HyDocFile hyDocFile) {

        return mongoTemplate.save(hyDocFile);
    }

}
